namespace SilverSea.Registration
{
    using SilverSea.Untilities;
    using System;
    using System.Windows.Forms;

    public class Register
    {
        private string activationKey = "";
        private int dayDemo = 30;
        private string fileActivationKey = "";
        private string fileDayInstall = "";
        private bool hideDemoButton = true;
        private string productCode = "";
        private string userCode = "";

        public Register(string fileDayInstall, string fileActivationKey, string productCode)
        {
            this.fileDayInstall = fileDayInstall;
            this.fileActivationKey = fileActivationKey;
            this.productCode = productCode;
            Author.WriteSecurityFile(fileDayInstall, DateTime.Now.Year.ToString("00") + "/" + DateTime.Now.Month.ToString("00") + "/" + DateTime.Now.Day.ToString("00"), false);
        }

        private int DayRemain()
        {
            try
            {
                DateTime time = Convert.ToDateTime(Author.ReadSecurityFile(this.fileDayInstall));
                long num = DateTimeUntils.DateDiff(DateInterval.Day, time, DateTime.Now.Date);
                return (this.dayDemo - ((int) Math.Abs(num)));
            }
            catch (Exception exception)
            {
                MessageBox.Show("Error while calculate day remain: " + exception.Message);
                return -1;
            }
        }

        public RunType ShowDialog()
        {
            this.userCode = Author.GetUserCode(this.productCode);
            this.activationKey = Author.ReadSecurityFile(this.fileActivationKey);
            if ((this.activationKey != null) && Author.IsGenuine(this.userCode, this.activationKey))
            {
                return RunType.Full;
            }
            frmRegistration registration = new frmRegistration {
                UserCode = this.userCode,
                ActivationKey = this.activationKey,
                HideDemoButton = this.hideDemoButton,
                DayDemo = this.dayDemo,
                DayRemain = this.DayRemain()
            };
            switch (registration.ShowDialog())
            {
                case DialogResult.OK:
                    Author.WriteSecurityFile(this.fileActivationKey, registration.ActivationKey, true);
                    this.activationKey = registration.ActivationKey;
                    return RunType.Full;

                case DialogResult.Retry:
                    return RunType.Demo;
            }
            return RunType.Expired;
        }

        public string ActivationKey
        {
            get
            {
                return this.activationKey;
            }
            set
            {
                this.activationKey = value;
            }
        }

        public int DayDemo
        {
            get
            {
                return this.dayDemo;
            }
            set
            {
                this.dayDemo = value;
            }
        }

        public bool HideDemoButton
        {
            set
            {
                this.hideDemoButton = value;
            }
        }

        public string UserCode
        {
            get
            {
                return this.userCode;
            }
            set
            {
                this.userCode = value;
            }
        }
    }
}

